package com.heng.futureDemo;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class thenCombineDemo15 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> completableFuture1 = CompletableFuture.supplyAsync(() -> {
            try {TimeUnit.MILLISECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
            return 10;
        });

        CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(() -> {
            try {TimeUnit.MILLISECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}
            return 20;
        });

        CompletableFuture<Integer> result = completableFuture1.thenCombine(completableFuture2, (x, y) -> {
            System.out.println("---开始两个结果合并");
            return x + y;
        });

        System.out.println(result.join());
    }


    @Test
    public void m1() {
        CompletableFuture<List<Integer>> listCompletableFuture = CompletableFuture.supplyAsync(() -> {
            List<Integer> list = new ArrayList<>();
            list.add(1);
            list.add(2);
            list.add(3);
            return list;
        });

        CompletableFuture<List<String>> listCompletableFuture1 = CompletableFuture.supplyAsync(() -> {
            List<String> list = new ArrayList<>();
            list.add("a");
            list.add("b");
            list.add("c");
            return list;
        });

        CompletableFuture<String> stringCompletableFuture = listCompletableFuture.thenCombine(listCompletableFuture1, (x, y) -> {
            System.out.println(x);
            System.out.println(x);
            return "ok";
        });
        String join = stringCompletableFuture.join();
        System.out.println(join);
    }
}
